package online.sixfish.common.utils.utils;


import com.alibaba.fastjson.JSON;
import online.sixfish.common.api.ApiErrorCode;
import online.sixfish.common.api.ApiResult;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class WebUtil
{
    /**
     * 将字符串渲染到客户端
     *
     * @param response 渲染对象
     * @param string 待渲染的字符串
     * @return null
     */
    public static String renderString(HttpServletResponse response, String string) {
        try
        {
            response.setStatus(200);
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            response.getWriter().print(string);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return null;
    }

    public static String renderTokenError(HttpServletResponse response){
        ApiResult<Object> result = new ApiResult<>(ApiErrorCode.TOKEN_EXPIRED);
        renderString(response, JSON.toJSONString(result));
        return null;
    }
    public static String renderTokenExpired(HttpServletResponse response){
        ApiResult<Object> result = new ApiResult<>(ApiErrorCode.UNAUTHORIZED);
        renderString(response, JSON.toJSONString(result));
        return null;
    }

    public static String renderPermissionDenied(HttpServletResponse response) {
        ApiResult<Object> result = new ApiResult<>(ApiErrorCode.FORBIDDEN);
        renderString(response, JSON.toJSONString(result));
        return null;
    }
}

